Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Graph*of mean and median of two variables over two variables

    Hi Stata users,

    I have two variables that I would like to compare their mean and median - var1 and var2 over two categorical variables - cat1 and cat2

    The code am using is
    Code:
    graph bar (mean) var1 (mean) var2 (median) var1 (median) var2, over(cat1) over(cat2)
    but am wondering whether there is a better way of presenting this information through graphs

    The data example is below

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float cat1 byte cat2 float(var1 var2)
    2 5   3     9
    2 2   .   -25
    2 5   .     0
    2 5   .   298
    1 5   .   363
    2 5   .    51
    2 1 155   331
    2 4   .    51
    2 4  24    37
    . 2   .     4
    2 2   0    -5
    2 1   9    63
    2 2 295   610
    2 1   .    21
    2 2  18    93
    2 5   1     3
    2 1 -68   109
    2 5 220   302
    2 1   .  -120
    2 1   8    19
    2 3  23    65
    2 1   .    72
    2 2  14   293
    2 1  32    31
    2 1   .   692
    2 1   .   -28
    2 5  65   101
    2 4   .     9
    1 5   .   163
    2 5  44   143
    2 4   .     1
    2 3  36   192
    2 5   .     0
    2 5  99   367
    2 5   .    47
    2 1 295   610
    2 4  11     1
    2 2 168   270
    . 5   .    64
    2 2   .    82
    2 2  23   102
    2 4   .    11
    2 1   .    66
    2 1   .    67
    1 5   .    91
    2 1 101    20
    2 3 385   847
    2 3  49    12
    2 3 295   385
    . 4   .   -20
    2 3   . -1389
    2 3   .   539
    2 3 299   -19
    2 4   .   404
    2 5  84   217
    1 5   .   382
    2 4   .   238
    2 2   .   -33
    2 4  23    88
    2 4   0     0
    . 3  40   153
    2 2  46     8
    2 5  12   100
    2 5   6   198
    2 5  35    91
    2 1  20    43
    2 2  60    80
    2 2  62   196
    2 3  28   129
    2 2  62    99
    2 4 150   491
    2 2  23   136
    2 3 433   557
    2 3  40    53
    2 2 579   364
    2 5 114   218
    2 1   .   144
    2 4 163     .
    1 3  10   459
    2 1  10    25
    2 4  81   116
    2 4   .   -12
    2 4   .   399
    2 1  27   189
    2 2   0     .
    2 4  45   359
    2 1   .   198
    2 5   .    92
    2 1  14   132
    2 2 124   161
    2 4   .   -75
    2 1   .    25
    2 5 -27   -26
    2 3   7   123
    2 3 350   594
    2 5   1     3
    2 3 219     .
    2 3   .    59
    2 3 225   600
    2 2 -99   -84
    end
    label values cat1 cat1
    label def cat1 1 "Low", modify
    label def cat1 2 "Medium", modify
    label values cat2 cat2
    label def cat2 1 "Q1", modify
    label def cat2 2 "Q2", modify
    label def cat2 3 "Q3", modify
    label def cat2 4 "Q4", modify
    label def cat2 5 "Q5", modify
    Thanks in advance!

  • #2
    Many possibilities. Here's one. (Hoping that the graph for the full dataset is more interesting than this!)

    Code:
    foreach v in var1 var2 { 
        foreach s in mean median { 
            egen `v'_`s' = `s'(`v'), by(cat1 cat2)
            label var `v'_`s' "`s' of `v'"
        }
    }
    
    gen cat2_1 = cat2 - 0.1 
    gen cat2_2 = cat2 + 0.1 
    
    scatter var1_* cat2_2,  ms(Oh X) || scatter var2_* cat2_1, xla(1 "Q1" 2 "Q2" 3 "Q3" 4 "Q4" 5 "Q5") by(cat1, note("")) ms(+ Th)

    Comment


    • #3
      Thanks so much Nick Cox for the proposed approach, I appreciate you taking time and sharing a solution

      Comment

      Working...
      X